// pages/topicality/xiaofeiquan/xiaofeiquan.js
//获取应用实例
const app = getApp()
const util = require("../../../utils/util.js");
var QQMapWX = require('../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
    key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
Page({

    /**
     * 页面的初始数据
     */
    data: {
        citycode: "sz",
        city: '深圳',
        province: '广东',
        pindex: 0,
        cindex: 0,
        cityPick: ["选择城市"],
        showtk: false,
        showNav: true, // 快捷导航是否展开
        djcolor: {
            "低风险": "#FFAA85",
            "中风险": "#CC2929",
            "高风险": "#660208"
        },
        showShare: true, // 显示分享按钮
        quValue: "",
        toolArr: [], //工具箱
        bannerArr: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        util.reportMessage("fangwenliang", {
            action: "疫情等级"
        });
        util.getUrl(app.globalData.url).then((res) => {
            this.getProvinceData(options.city);
            if (options.qu)
                this.setData({
                    quValue: options.qu
                })
        })
    },
    // 跳转主页
    toHome() {
        ks.reLaunch({
            url: '/pages/index/index?city=' + this.data.citycode,
        })
    },
    // 获取工具箱
    getTools(citycode) {
        util.request('zhuanti.php', {
            action: "geli_tools",
            citycode: citycode
        }, "POST").then((e) => {
            this.setData({
                toolArr: e.data.data
            })

        })
    },
    // 获取轮播banner
    getBanners(citycode) {
        util.request('citycode.php', {
            action: 'lunbo',
            citycode: citycode
        }, 'POST').then(e => {
            this.setData({
                bannerArr: e.data.data
            })
        })
    },
    // 前往工具箱详情
    toTool(e) {
        let path = e.currentTarget.dataset.path;
        let linkname = e.currentTarget.dataset.title;
        let appid = e.currentTarget.dataset.appid || '';
        if (path) {
            if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1 && !appid) {
                util.navContact(linkname, path)
            } else {
                util.navMethod(path, appid);
            }
        } else {
            ks.showToast({
                title: '还没有这个入口哦~',
                icon: 'none'
            })
        }
    },
    //banner详情
    toBannerDetail(e) {
        let path = e.currentTarget.dataset.path;
        let linkname = e.currentTarget.dataset.title;
        let appid = e.currentTarget.dataset.appid || '';
        if (path) {
            if (path.indexOf("pages") == -1 && path.indexOf("mp.weixin.qq.com") == -1 && !appid) {
                util.navContact(linkname, path)
            } else {
                util.navMethod(path, appid);
            }
        } else {
            ks.showToast({
                title: '还没有这个入口哦~',
                icon: 'none'
            })
        }
    },
    // 分享按钮
    shareBtn(e) {
        this.setData({
            showModalStatus: true,
            openShare: false,
        })
    },
    openShare() {
        this.setData({
            openShare: true,
        });
    },
    closeShare() {
        this.setData({
            openShare: false,
        })
    },
    // 定位
    getLocation() {
        qqmapsdk.reverseGeocoder({
            success: (res) => {
                let city = res.result.address_component.city;
                // let city="芜湖市";
                if (city) {
                    let citydata = this.data.citylist.filter(v => {
                        if (city.indexOf(v.cityname) != -1) {
                            return v;
                        }
                    });

                    if (citydata != '' && city.indexOf(this.data.city) == -1) {
                        ks.showModal({
                            content: '您当前定位城市为' + city + '\n是否需要切换到' + city + '疫情等级查询？',
                            success: res => {
                                if (res.confirm) {
                                    // let citydata = this.data.citylist.filter(v => {
                                    //   if (city.indexOf(v.cityname) != -1) {
                                    //     return v;
                                    //   }
                                    // });
                                    this.setData({
                                        city: Array.isArray(citydata) ? citydata[0].cityname : this.data.city,
                                        citycode: Array.isArray(citydata) ? citydata[0].citycode : this.data.citycode,
                                        province: Array.isArray(citydata) ? citydata[0].province : this.data.province,
                                        cityPick: ["选择城市"]
                                    })
                                    let clist = [];
                                    this.data.provPick.forEach((v, k) => {
                                        if (v == this.data.province) {
                                            this.setData({
                                                pindex: k,
                                            })
                                        }
                                    })
                                    this.data.cityArray[this.data.pindex].citylist.forEach(v => {
                                        clist.push(v.cityname)
                                    })
                                    this.setData({
                                        cityPick: this.data.cityPick.concat(clist)
                                    })
                                    this.data.cityPick.forEach((v, k) => {
                                        if (v == this.data.city) {
                                            this.setData({
                                                cindex: k,
                                            })
                                        }
                                    })
                                    // 获取城市公众号二维码
                                    this.getQrcode(this.data.citycode);
                                    this.getInfoData(this.data.citycode);
                                }
                            }
                        })
                    }
                }

            },
        });
    },

    // 获取城市公众号二维码
    getQrcode(citycode) {
        util.cityConfig(citycode).then(res => {
            this.setData({
                qrcodeimg: res.qrcode,
            })
        })
    },

    // 获取有疫情等级的城市
    getProvinceData(optcode) {
        let data = {
            action: 'yqdengji',
            type: "city",
        };
        util.request('zhuanti.php', data, ).then(res => {
            this.setData({
                cityArray: res.data.data.city,
            })
            let provPick = [];
            let citylist = [];
            let clist = [];
            this.data.cityArray.forEach(v => {
                v.citylist.forEach(v => {
                    citylist.push(v)
                })
                provPick.push(v.province)
            });

            this.setData({
                provPick,
                citylist
            })
            if (this.data.citylist) {
                this.data.citylist.forEach((v, k) => {
                    if (optcode == v.citycode) {
                        this.data.citycode = v.citycode;
                        this.data.city = v.cityname;
                        this.data.province = v.province
                    } else {
                        this.data.citycode = this.data.citycode;
                        this.data.city = this.data.city;
                        this.data.province = this.data.province;

                    }
                })
                this.setData({
                    citycode: this.data.citycode,
                    city: this.data.city,
                    province: this.data.province,
                })
            }

            this.data.provPick.forEach((v, k) => {
                if (v == this.data.province) {
                    this.setData({
                        pindex: k,
                    })
                }
            })

            this.data.cityArray[this.data.pindex].citylist.forEach(v => {
                clist.push(v.cityname)
            })

            this.setData({
                cityPick: this.data.cityPick.concat(clist)
            })

            this.data.cityPick.forEach((v, k) => {
                if (v == this.data.city) {
                    this.setData({
                        cindex: k,
                    })
                }
            })
            // 获取城市公众号二维码
            this.getQrcode(this.data.citycode);

            this.getTools(this.data.citycode)
            this.getBanners(this.data.citycode)
            this.getInfoData();

            if (this.data.citylist != "" && Array.isArray(this.data.citylist) && !optcode) {
                // 获取定位
                this.getLocation();
            }
        })
    },

    // 获取数据
    getInfoData(citycode) {
        let data = {
            action: 'yqdengji',
            citycode: citycode ? citycode : this.data.citycode
        };
        util.request('zhuanti.php', data, ).then(res => {
            if (res.data.data.daofen) {
                res.data.data.daofen.content = res.data.data.daofen.content
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(="[^"]+")))/ig, '<p')
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p')
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(class="[^"]+")))/ig, '<p')
                    .replace(/<p>/ig, '<p class="p_class">')
                    .replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1')
                    .replace(/<img/gi, '<img style="width:100%;height:auto"')
                // .replace(/<span>/ig, '<p class="p_class">')
            }
            ks.setNavigationBarTitle({
                title: res.data.data ? res.data.data.title : "疫情等级",
            })
            this.setData({
                infoData: res.data.data,
                quList: res.data.data.dengji,
                updateTime: util.timestamp(res.data.data.update_at, 'Y-M-D h')
            });
            if (this.data.quValue)
                this.searchData(this.data.quValue)
        })
    },

    // 搜索区域
    searchData(value) {
        let searchRes = [];
        if (this.data.quList) {
            searchRes = this.data.infoData.dengji.filter(v => {
                if (v.qu.includes(value)) {
                    return v;
                }
            })
        }
        if (searchRes != '') {
            this.setData({
                quList: searchRes
            })
        } else {
            ks.showModal({
                content: '未找到相关区域信息，请重新查询',
                showCancel: false,
                confirmText: "我知道了"
            })
        }

    },

    // 省份选择
    bindProv: function (e) {
        this.setData({
            pindex: e.detail.value,
            cindex: 0,
            cityPick: ["选择城市"],
        })
        let clist = [];
        this.data.cityArray[this.data.pindex].citylist.forEach(v => {
            clist.push(v.cityname)
        })
        this.setData({
            cityPick: this.data.cityPick.concat(clist),
        })
    },

    // 城市选择
    bindCity(e) {
        this.setData({
            cindex: e.detail.value
        })
        let cityname = this.data.cityPick[this.data.cindex];
        if (this.data.citylist) {
            this.data.citylist.forEach(v => {
                if (v.cityname == cityname) {
                    let citycode = v.citycode;
                    this.setData({
                        citycode,
                        city: cityname
                    })
                }
            })
        }
        // 获取城市公众号二维码
        this.getQrcode(this.data.citycode);
        this.getInfoData(this.data.citycode);
    },

    // 进入客服会话
    contact() {
        this.setData({
            showtk: false,
        })
    },

    // 关闭
    close() {
        this.setData({
            showtk: false,
        })
    },

    // 搜索输入事件
    inputQu(e) {
        let quValue = e.detail.value;
        this.setData({
            quValue,
        })
    },
    // 搜索区域
    searchQu(e) {
        let quValue = e.detail.value;
        this.setData({
            quValue,
        })
        this.searchData(quValue)
    },

    // 查询按钮
    searchBtn() {
        this.searchData(this.data.quValue)
    },

    /**
     * 页面滑动事件的处理函数
     */
    onPageScroll: function (e) {
        this.setData({
            showNav: false,
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: this.data.infoData ? this.data.infoData.title : "疫情等级查询",
        }
    },

    // 分享朋友圈
    onShareTimeline: function () {
        return {
            title: this.data.infoData ? this.data.infoData.title : "疫情等级查询",
            imageUrl: "http://imgbdb3.bendibao.com/xcx/20205/20/2020520151108_56661.jpg",
            query: {
                city: this.data.citycode
            },
        }
    },
})